package ns.gflex.domain

import ns.gflex.util.Encoder;
import ns.gflex.domain.association.UserRole;

class User {
	String account
	String password
	String name
	Boolean editable = true
	Boolean enabled = true
	Department dept;
	static mapping = {
		dept fetch:'join',lazy:false
	}
	static constraints = { account unique:true }

	static init = {
		def dept = Department.findByName('临时部门')
		def admin = new User(account:'admin',name:'管理员',dept:dept,editable:false,password:Encoder.MD5Encoder.encode('admin')).save()
		def anonymous = new User(account:'anonymous',name:'匿名帐号',dept:dept,editable:false,password:Encoder.MD5Encoder.encode('anonymous')).save()

		def roleAdmin = Role.findByRoleCode('Administrators')
		def rolePublic = Role.findByRoleCode('Public')

		new UserRole(user:admin,role:roleAdmin).save()
		new UserRole(user:admin,role:rolePublic).save()
		new UserRole(user:anonymous,role:rolePublic).save()
	}
}
